﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrawlerCore.Helper
{
    class DateTimeHelper
    {
        /// <summary>
        /// tra ve xau ca ngay va gio
        /// </summary>
        /// <param name="date">ngay di hoac den</param>
        /// <param name="time">gio khoi hanh hoac ha canh vd: 06:00(+1)</param>
        /// <returns></returns>
        public static string getDate(DateTime date, string time)
        {
            string r = "";
            time = time.Trim();
            try
            {
                int index = time.IndexOf('(');
                string addDay = "";
                if (index > -1)
                {
                    addDay = time.Substring(index + 1);
                    addDay = addDay.Replace(')', ' ');
                }
                else
                {
                    index = time.Length;
                }
                DateTime time1 = DateTime.Parse(time.Substring(0, index));
                DateTime d = new DateTime(date.Year, date.Month, date.Day, time1.Hour, time1.Minute, 0);
                int addDayInt = 0;
                if (int.TryParse(addDay, out addDayInt))
                {
                    d = d.AddDays(addDayInt);
                }

                r = d.ToString();
            }
            catch { }
            return r;
        }
        /// <summary>
        /// tra ve ngay gio day du tu trang abacus
        /// </summary>
        /// <param name="departDate"></param>
        /// <param name="time">dang 12:23 PM</param>
        /// <param name="layOver">dang 3h 30m,neu khong can thi truyen vao string.Empty</param>
        /// <returns></returns>
        public static DateTime getDateAbacusDepartTime(DateTime departDate, string time, string layOver)
        {
            try
            {
                int h = 0, m = 0;
                if (layOver != string.Empty)
                {
                    int indexOfh = layOver.IndexOf('h');
                    string hString = layOver.Substring(0, indexOfh);
                    int t;
                    if(!int.TryParse(hString.Substring(0,1),out t))
                    {
                        hString = hString.Remove(0, 1);
                    }
                    h = int.Parse(hString);
                    m = int.Parse(layOver.Substring(indexOfh + 1, layOver.IndexOf('m') - indexOfh - 1));
                }
                DateTime departTemp = departDate.AddHours(h);
                departTemp = departTemp.AddMinutes(m);
                DateTime timeTime = DateTime.Parse(time);
                DateTime result = new DateTime(departTemp.Year, departTemp.Month, departTemp.Day, timeTime.Hour, timeTime.Minute, 0);
                return result;
            }
            catch
            {
                return new DateTime();
            }
        }
        public static DateTime getDateAbacusArriveTime(DateTime departDate, string time)
        {
            try
            {
                DateTime timeTime = DateTime.Parse(time);
                DateTime result = new DateTime(departDate.Year, departDate.Month, departDate.Day, timeTime.Hour, timeTime.Minute, 0);
                if (timeTime.Hour < departDate.Hour)
                {
                    result = result.AddDays(1);
                }
                return result;
            }
            catch 
            {
                return new DateTime();
            }
        }
    }
}
